package com.chf.job.service.impl;

import com.chf.job.dao.ProductMapper;
import com.chf.job.pojo.Product;
import com.chf.job.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    /**
     * 根据公司id获取产品集合
     * @param id
     * @return
     */
    @Override
    public List<Product> getByCompanyId(Integer id) {
        return productMapper.getByCompanyId(id);
    }

    /**
     * 添加公司产品
     * @param id
     * @param product
     * @return
     */
    @Override
    public int addProduct(Integer id, Product product) {
        if (product.getId() == null) {
            // 创建新的产品
            Product newProduct = new Product();
            newProduct.setCompanyId(id);
            newProduct.setProductName(product.getProductName());
            newProduct.setProductSummary(product.getProductSummary());
            newProduct.setProductUrl(product.getProductUrl());
            newProduct.setCreateTime(product.getCreateTime());
            newProduct.setUpdateTime(product.getUpdateTime());
            return productMapper.insertSelective(newProduct);
        }
        // 修改产品
        Product newProduct = new Product();
        product.setId(product.getCompanyId());
        newProduct.setCompanyId(id);
        newProduct.setProductName(product.getProductName());
        newProduct.setProductSummary(product.getProductSummary());
        newProduct.setProductUrl(product.getProductUrl());
        newProduct.setCreateTime(product.getCreateTime());
        newProduct.setUpdateTime(product.getUpdateTime());
        return productMapper.updateByPrimaryKeySelective(newProduct);

    }
}
